import os
import sys
import cv2
from Utils.colors import *
from Utils.color_utils import *


class River:
    def __init__(self):
        self.river_color_lower = [0, 43, 46]
        self.river_color_upper = [10, 255, 255]

    def get_color_lower_upper(self, color: ColorRange()):
        river_color = color.get_river_color()  # [(),(),()]
        self.river_color_lower, self.river_color_upper = Utils.get_colors_lower_upper(river_color)

    def read_river_contours(self, color, image_hsv):
        self.get_color_lower_upper(color)
        self.river_color_lower, self.river_color_upper = Utils.list_transform_array(self.river_color_lower, self.river_color_upper)
        mask = cv2.inRange(image_hsv, self.river_color_lower, self.river_color_upper)

        # 中值滤波
        result = cv2.medianBlur(mask, 3)

        # 进行二值化
        ret, thread = cv2.threshold(result, 0, 255, cv2.THRESH_BINARY)
        contours, hierarchy = cv2.findContours(thread, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)  # 坐标, 关系
        return contours
